import {ref} from "vue";
import {onLoad, onPageScroll, onShow} from "@dcloudio/uni-app";
import {challengeNumAPI} from "../services/lstz";
import {useUserInfoStore} from "../stores/modules/userInfo";
import {getUserInfoAPI} from "../services/me";

export const bindBoxPosterHook = () => {
    const isShowBackHome = ref(false)
    const goBack = () => {
        if (isShowBackHome.value) {
            uni.switchTab({url: '/pages/index/index'})
        } else {
            uni.navigateBack()
        }
    }

    onLoad(() => {
        getChallengeNumAPI()
        // imagesJsonAPI()
    })

    onShow(() => {
        let pages = getCurrentPages()
        isShowBackHome.value = pages.length === 1;
    })
    const userInfoStore = useUserInfoStore()
    const countData = ref({})
    const successList = ref([])
    const getChallengeNumAPI = async () => {
        const res = await challengeNumAPI(userInfoStore.userId, "贵阳市")
        countData.value = res.result
        countData.value?.list.forEach((item) => {
            successList.value.push(item.message)
        })
    }
    const listImageC = ref()
    // 动画是否切换
    const isChange = ref(false)
    // 是否显示抽奖弹框
    const isfShowGif = ref(false)
    // 是否显示阻力成功弹框
    const isfShowZLSuc = ref(false)
    // 是否显示阻力弹框
    const isfShowZL = ref(false)
    const listImage = ref([
        [
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"}
        ],
        [
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_one.png"},
            {"img": "https://lsz.lszbg.com/imgs/raffle/gif_tow.png"}
        ]
    ])
    const mhCount = ref(9)

    onLoad(() => {

    })
    // 动画切换
    const setRotate = () => {
        if (listImageC.value === listImage.value[0]) {
            listImageC.value = listImage.value[1]
            isChange.value = true
        } else {
            listImageC.value = listImage.value[0]
            isChange.value = false
        }
    }
    // 动画时间
    const timeCount = ref(1000)
    // 循环动画时间
    const timeInterval = ref()
    // 动画开始
    const cuntImage = () => {
        timeInterval.value = setInterval(() => {
            setRotate()
        }, timeCount.value)
    }
    cuntImage()
    // 抽奖动画切换
    const mhcj = () => {
        if (timeCount.value === 88) {
            return
        }
        if (mhCount.value === 0) {
            uni.showToast({title: '抽奖次数不足', icon: 'none'})
            return
        }
        mhCount.value -= 1
        timeCount.value = 88
        clearInterval(timeInterval.value)
        cuntImage()
        setTimeout(() => {
            isfShowGif.value = true
            timeCount.value = 1000
            clearInterval(timeInterval.value)
            cuntImage()
        }, 2000)
    }
    // 跳转红包页面
    const toRedBag = () => {
        uni.navigateTo({url: "/pagesme/red_bag"})
    }
    // 跳转阻力列表
    const toBlindBoxHelpList = () => {
        uni.navigateTo({url: "/pages_gold/blind_box_help_list"})
    }
    // 跳转海报生成页面
    const toBlindBoxPoster = () => {
        uni.navigateTo({url: "/pages_gold/blind_box_poster"})
    }
    // 点击助力按钮
    const zhuLi = () => {
        isfShowZL.value = false
        isfShowZLSuc.value = true
    }
    const scrollTop = ref(0)
    onPageScroll(e => {
        // scrollTop.value = e.scrollTop
        // console.log("scrollTop.value",e.scrollTop)
    })
    const onScroll = (e) => {
        scrollTop.value = e.detail.scrollTop
        // console.log("scrollTop.value1",e.detail.scrollTop)
    }
    onShow(async () => {
        if (!userInfoStore.isLoginBool()) {
            // 登录后获取用户信息
            await uni.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        // 获取用户信息
        await getUserInfo()
    })
    // 判断用户是否登录，是否绑定手机号
    const getUserInfo = async () => {
        const res = await getUserInfoAPI()
        userInfoStore.setUserInfo(res.result)
        // 是否绑定手机号
        if (!userInfoStore.userInfo.username) {
            uni.showModal({
                title: '绑定手机号-提示',
                content: '您还未绑定手机号，手机号用于抢单、订单核验，和订单状态通知，是否去绑定？',
                confirmText: '去绑定',
                success: async function (res) {
                    if (res.confirm) {
                        await uni.navigateTo({
                            url: "/pagesme/settinginfo"
                        })
                    }
                }
            })
        } else {
            // 如果有绑定手机号就可以显示助力弹框
            isfShowZL.value = true
        }
    }
    // 一开始就是一个错的做法结果导致一个错的结果，一个错的结果没有止损导致错上加错。
    const html1 = ref('<view style="font-size: 28rpx;color: #3D3D3D;line-height: 49rpx;font-weight: bold;letter-spacing: 5rpx">' +
        '<view>宝，欢迎来到“领食周边购”，这是一个能为您每一餐外卖省钱的贴心小工具。感谢您为您的朋友XXXX助力开启言盒!</view>' + '<br>' + '<br>' +
        '<view>开启成功后Ta会获得平台赠送的神秘礼物，而您也可以获得在</view><view style="color: #D8030F">“领食周边购”0元吃外卖的体验福利一次</view>' +
        '</view>')
    const html = ref('<view style="font-size: 28rpx;color: #3D3D3D;line-height: 44rpx;">恭喜您助力成功，神秘礼物正在加速与您的朋友汇合，您获得恭喜您助力成功，神秘礼物正在加速与您的朋友汇合，您获得恭喜您助力成功，神秘礼物正在加速与您的朋友汇合，您获得<text style="color: #D8030F">领食周边购“新用户0元体验”1次，美团，饿了么全平台覆盖，优质外卖0元畅享。</text>截图保存扫描下方二维码@管理员领取</view>')

    return {
        scrollTop,
        onScroll,
        html,
        html1,
        isfShowZL,
        isfShowZLSuc,
        isfShowGif,
        listImage,
        listImageC,
        isChange,
        successList,
        mhCount,
        mhcj,
        goBack,
        toRedBag,
        toBlindBoxHelpList,
        toBlindBoxPoster,
        zhuLi,
        setRotate,
        cuntImage,
        timeCount,
        timeInterval,
        isShowBackHome
    }
}